---@装饰器 请求的基础处理
local _base = class("BaseDecorator")
--======================================
--default params
local d = Ding
local ngx_req = ngx.req
local Logger = d.Logger
local json = require("cjson")
-- local HTTPMETHOD = require("framework.constant").HTTPMETHOD
--======================================
-- 枚举 基础的检查报错
local ENUM_DECODE_ERROR = {
    ["DECODE_ERROR"] = 1,
    ["PARAMS_ERROR"] = 2,
    ["CONTENT_EMPTY"] = 3
}
local ENUM_ERROR_TIPS = {
    ["DECODE_ERROR"] = "json decode error",
    ["PARAMS_ERROR"] = "params nec-keys not found",
    ["CONTENT_EMPTY"] = "params contnet is empty"
}

local EXPORTED_METHODS = {
    "get_url_params_",
    "get_body_params_",
    "body_jsonparams_nec_",
    "url_jsonparams_nec_"
}

function _base:init_()
end

function _base:bind(target)
    self:init_()
    d.setmethods(target, self, EXPORTED_METHODS)
    self._s = target
    
end

function _base:unbind(target)
    d.unbind(target, EXPORTED_METHODS)
end

function _base:get_url_params_()
    local reqdata = ngx.req.get_uri_args() or {}
    return reqdata
end

function _base:get_body_params_()
    ngx_req.read_body()
    local contbody = ngx_req.get_body_data()
    local err_code, reqdata
    d.Logger:info("Cont body-->>%s", contbody)
    if not empty(contbody) then
        try {
            function()
                reqdata = json.decode(contbody)
            end,
            catch {
                function(err)
                    Logger:error("json decode error!")
                end
            }
        }
        if not reqdata then
            err_code = 1
        end
    end
    return err_code, reqdata
end

function _base:body_jsonparams_nec_(neckey)
    ngx_req.read_body()
    local contbody = ngx_req.get_body_data()
    local err_key, reqdata
    d.Logger:info("Cont body-->>%s", contbody)
    if not empty(contbody) then
        try {
            function()
                reqdata = json.decode(contbody)
            end,
            catch {
                function(err)
                    Logger:error("json decode error!")
                end
            }
        }
        if not reqdata then
            err_key = "DECODE_ERROR"
        else 
            if (type(neckey) == "table") then 
                for _, key in ipairs(neckey) do 
                    if not (reqdata[key]) then 
                        err_key = "PARAMS_ERROR"
                    end
                end
            end
        end
    else 
        err_key = "CONTENT_EMPTY"
    end
    if (err_key) then
        self._s:doerror(ENUM_DECODE_ERROR[err_key], ENUM_ERROR_TIPS[err_key])
        return false
    else 
        return true, reqdata
    end
end

function _base:url_jsonparams_nec_(neckey)
    local uri_params = ngx.req.get_uri_args() or {}
    local err_key, reqdata
    d.Logger:dump(uri_params, "Cont body-->>")
    if (type(neckey) == "table") then 
        for _, key in ipairs(neckey) do 
            if not (uri_params[key]) then 
                err_key = "PARAMS_ERROR"
            end
        end
    end
    if (err_key) then
        self._s:doerror(ENUM_DECODE_ERROR[err_key], ENUM_ERROR_TIPS[err_key])
        return false
    else 
        return true, uri_params
    end
end

return _base
